<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of UserController
 *
 * @author MinhNguyen
 */
class UserController {

    private $model;
    private $view;
    
    public function __construct($model, $view) {
        $this->model = $model;
        $this->view = $view;
    }
    
    /**
     * To check password when user login
     * if it's true, display jsonView.php
     * if is's false, display Wrong message
     */
    public function login($password) {
        if ($password != null)
            if ($password == $this->model->password) {
                $this->view->status = new Status();
                $this->view->status->code = 200;
                $this->view->status->message = "OK";
                $this->view->result = $this->model;
                $this->view->display("jsonView.php");
            }
            else {
                $controller = new ErrorController(new Error(), new ErrorView(), 
                    "401", "Wrong password");
                $controller->returnError();
            }
    }
    
    /**
     * To save User to Database 
     * if it's New User, insert User to Users table
     * if is's Old User, update it 
     */
    public function Save() {
        $model = $this->model;
        $query = "SELECT * FROM Users WHERE ID = $model->id";
        $result = Database::getInstance()->query($query);
        if ($result->returnCode == 200)  //OK
            if ($result->dataTable->numOfRow > 0) {
                Database::getInstance()->query("UPDATE Users
                        SET name='$model->name', 
                        password='$model->password', 
                        email='$model->email', 
                        phone='$model->phone', 
                        address='$model->address', 
                        company='$model->company', 
                        position='$model->position', 
                        status ='$model->status', 
                        dateregistered ='$model->dateregistered', 
                        token ='$model->token' 
                        WHERE ID = $model->id");
                return true;
            }
            else {
                Database::getInstance()->query("INSERT INTO Users VALUES
                            ('$model->id', '$model->name', 
                            '$model->password', '$model->email', 
                            '$model->phone', '$model->address', 
                            '$model->company', '$model->position', 
                            '$model->status', '$model->dateregistered', 
                            '$model->token')");
                return true;
            }
    }
    
    /**
     * To Delete User by own in Users Table (database)
     */
    public function Delete() {
        $query = "DELETE FROM Users WHERE ID = $model->id";
        Database::getInstance()->query($query);
        return true;
    }
}
?>
